package org.springblade.modules.admin.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.BuildingElevatorDTO;
import org.springblade.modules.admin.entity.BuildingElevatorVisitor;
import org.springblade.modules.admin.mapper.BuildingElevatorVisitorMapper;
import org.springblade.modules.admin.service.IBuildingElevatorVisitorService;
import org.springblade.modules.admin.vo.BuildingAlreadyVO;
import org.springblade.modules.admin.vo.BuildingElevatorVO;
import org.springblade.modules.admin.vo.BuildingElevatorVisitorVO;
import org.springblade.modules.admin.vo.ElevatorDetailVO;
import org.springblade.modules.admin.vo.elevator.ElevatorDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;


/**
 * @author: Sharry
 * @createTime: 2023/4/10 10:27
 * @version: Version-1.0
 */
@Slf4j
@Service
public class BuildingElevatorVisitorServiceImpl extends BaseServiceImpl<BuildingElevatorVisitorMapper, BuildingElevatorVisitor> implements IBuildingElevatorVisitorService {

	/**
	 * 获取项目-电梯列表，可根据项目名模糊查询
	 * @param
	 * @return 项目-电梯列表
	 */
	@Override
	public List<BuildingElevatorVO> getListByBuildingName(Map<String,String> param) {
		// 获取项目-电梯列表
		return baseMapper.getListByBuildingName(param);
	}

	/**
	 * 增加 项目-电梯可报障电梯
	 *
	 * @param buildingElevatorList 项目-电梯列表
	 * @return 是否成功
	 */
	@Override
	public Boolean addElevators(List<BuildingElevatorDTO> buildingElevatorList) {
		List<BuildingElevatorVisitor> bevList = new ArrayList<>();
		// 复制属性并插入
		buildingElevatorList.forEach(b -> {
			if (!Func.isNull(b)) {
				// 先判断是否已存在该电梯，是，删除再插入
				Long elevatorId = b.getElevatorId();
				if(!Func.isNull(elevatorId)){
					BuildingElevatorVisitor buildingElevatorVisitor =
						getOne(new LambdaQueryWrapper<BuildingElevatorVisitor>()
							.eq(BuildingElevatorVisitor::getElevatorId, elevatorId));
					// 非空，先删除
					if(!Func.isNull(buildingElevatorVisitor)){
						remove(new LambdaQueryWrapper<BuildingElevatorVisitor>()
							.eq(BuildingElevatorVisitor::getElevatorId,elevatorId));
					}
				}
				BuildingElevatorVisitor bve = new BuildingElevatorVisitor();
				BeanUtils.copyProperties(b, bve);
				bevList.add(bve);
			}
		});
		return saveOrUpdateBatch(bevList);
	}

	/**
	 * 根据已选，获取向后端提交的DTO
	 * @param buildingElevatorList 获取依据
	 * @return 获取向后端提交的DTO
	 */
	@Override
	public List<BuildingElevatorDTO> getBuildingElevatorSelected(List<BuildingElevatorVO> buildingElevatorList) {
		List<BuildingElevatorDTO> result = new ArrayList<>();
		// 根据提交信息，依次获取DTO
		if (Func.isNotEmpty(buildingElevatorList)) {
			buildingElevatorList.forEach(b -> {
				List<ElevatorDetailVo> detailList = b.getChildren();
				if (Func.isNotEmpty(detailList)) {
					detailList.forEach(d -> {
						BuildingElevatorDTO beDTO = new BuildingElevatorDTO();
						beDTO.setBuildingId(Convert.toLong(b.getBuildingId()));
						beDTO.setBuildingName(b.getBuildingName());
						beDTO.setElevatorAddress(d.getElevatorAddress());
						beDTO.setElevatorId(Convert.toLong(d.getElevatorId()));
						result.add(beDTO);
					});
				}
			});
		}
		// 去重
		log.info("去重前{}",result.size());
		List<BuildingElevatorDTO> finalResult = result.stream().filter(distinctByKey(BuildingElevatorDTO::getElevatorId)).collect(Collectors.toList());
		log.info("去重后{}",finalResult.size());
		return finalResult;
	}

	/**
	 * 根据项目id 获取 已添加部分游客可报障电梯
	 * @param buildingId 项目id
	 * @return 可报障电梯列表
	 */
	@Override
	public List<BuildingElevatorVisitorVO> getVisitorsElevatorsById(Long buildingId) {
		List<BuildingElevatorVisitor> bevList = list(new LambdaQueryWrapper<BuildingElevatorVisitor>().eq(BuildingElevatorVisitor::getBuildingId, buildingId));
		List<BuildingElevatorVisitorVO> result = new ArrayList<>();
		// 复制属性
		bevList.forEach(b ->{
			BuildingElevatorVisitorVO beVO = new BuildingElevatorVisitorVO();
			BeanUtils.copyProperties(b,beVO);
			result.add(beVO);
		});
		return result;
	}

	@Override
	public List<BuildingAlreadyVO> getAlready() {
		// 查询全部已添加项目
		return baseMapper.getBuildingElevatorCount();
	}

	@Override
	public Boolean removeByBuildingId(Long id) {
		return remove(new LambdaQueryWrapper<BuildingElevatorVisitor>().eq(BuildingElevatorVisitor::getBuildingId, id));
	}

	@Override
	public Boolean removeByElevatorIds(List<Long> elevatorIds) {
		for(Long eId : elevatorIds){
			remove(new LambdaQueryWrapper<BuildingElevatorVisitor>().eq(BuildingElevatorVisitor::getElevatorId,eId));
		}
		return Boolean.TRUE;
	}

	@Override
	public List<BuildingElevatorVO> getTree() {
		return baseMapper.getTree();
	}

	/**
	 * 物理删除全部
	 * @param tenantId
	 */
	@Override
	public void deleteALL(String tenantId) {
		baseMapper.deleteAll(tenantId);
	}

	public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
		Map<Object, Boolean> seen = new ConcurrentHashMap<>();
		return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
	}
}
